package com.tiger.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Base64;

import org.apache.commons.lang3.StringUtils;

/**
 * 
 * @author andy
 *
 */
public class ImageUtils {
	public static String saveImage(String fileName, byte[] data) throws IOException {
		try(FileOutputStream os = new FileOutputStream(new File(Conf.get("driver.picture.path") + fileName))) {
			os.write(data);
		}
		return fileName;
	}
	
	public static byte[] getImageData(String img) throws IOException {
		String imgData = img.substring(img.indexOf("base64,") + "base64,".length());
		return Base64.getDecoder().decode(imgData);
	}
	
	
	public static boolean isImage(String img) {
		return StringUtils.isNotEmpty(getImageType(img));
	}
	
	public static String getImageType(String img) {
		String imgType = img.substring(img.indexOf("data:") + "data:".length(), img.indexOf(";"));
		switch(imgType) {
		case "image/png": return "png";
		case "image/jpg": return "jpg";
		case "image/jpeg": return "jpeg";
		default: return null;
		}
	}
}
